package com.example.networkinteraction.retrofit;

import com.example.networkinteraction.convert.ToStringConverterFactory;
import com.example.networkinteraction.interceptor.HttpLogger;
import com.example.networkinteraction.util.HttpUtils;
import com.google.protobuf.ExtensionRegistryLite;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.CallAdapter;
import retrofit2.Converter;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.protobuf.ProtoConverterFactory;

import java.util.concurrent.TimeUnit;

import static com.example.networkinteraction.constant.NetworkContant.TIME_OUT;

/**
 * Description:
 * File: RetrofitBuilder.java
 * Author: Administrator
 * Create: 2019/3/30 0030 上午 11:07
 */
public class RetrofitBuilder {
    private String baseUrl;
    private CallAdapter.Factory[] callAdapterFactory;
    private Converter.Factory[] convertFactory;
    private OkHttpClient okHttpClient;

    public RetrofitBuilder setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
        return this;
    }

    public RetrofitBuilder setCallAdapterFactory(CallAdapter.Factory... callAdapterFactory) {
        this.callAdapterFactory = callAdapterFactory;
        return this;
    }

    public RetrofitBuilder setConvertFactory(Converter.Factory... convertFactory) {
        this.convertFactory = convertFactory;
        return this;
    }

    public RetrofitBuilder setOkHttpClient(OkHttpClient okHttpClient) {
        this.okHttpClient = okHttpClient;
        return this;
    }

    public Retrofit builder() {
        Retrofit.Builder builder = new Retrofit.Builder();
        builder.baseUrl(baseUrl);

        if (null == callAdapterFactory || callAdapterFactory.length <= 0) {
            builder.addCallAdapterFactory(RxJava2CallAdapterFactory.create());
        } else {
            for (CallAdapter.Factory factory : callAdapterFactory) {
                builder.addCallAdapterFactory(factory);
            }
        }

        if (null == convertFactory || convertFactory.length <= 0) {
            builder.addConverterFactory(ToStringConverterFactory.create());
            builder.addConverterFactory(GsonConverterFactory.create());
            builder.addConverterFactory(ProtoConverterFactory.createWithRegistry(ExtensionRegistryLite.newInstance()));
        } else {
            for (Converter.Factory factory : convertFactory) {
                builder.addConverterFactory(factory);
            }
        }
        if (null == okHttpClient) {
            builder.client(createOkHttpClient());
        } else {
            builder.client(okHttpClient);
        }
        return builder.build();
    }

    private OkHttpClient createOkHttpClient() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();

        builder.readTimeout(TIME_OUT, TimeUnit.SECONDS);
        builder.writeTimeout(TIME_OUT, TimeUnit.SECONDS);
        builder.connectTimeout(TIME_OUT, TimeUnit.SECONDS);

        HttpUtils.SSLParams sslParams = HttpUtils.getSslSocketFactory();
        builder.sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager);

        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLogger());
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        builder.addInterceptor(loggingInterceptor);

        return builder.build();
    }
}
